﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Windows;
using System.Text.RegularExpressions;
using NHunspell;

namespace InscriberDataManager
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        int indexlocation = -1;
        Functionality.Reports rpselected = null;

        System.Windows.Controls.ListBox dragSource = null;
        object[] dragObject=null;
        int dragObjectCount = 0;
        int dragType = 0;

        public Window1()
        {
            InitializeComponent();
           // this.InitializeDynamic();
            Functionality.readXMLParameters();
            Functionality.getreports(listBox3);
            Functionality.getRuningLines(listBox2,"VISOS");
            Functionality.getRuningLinesTypes(comboBox1);
            Functionality.getLogoTemplates(comboBox2);
            Functionality.getAnonsaiPartsByANId("1", listBox10);
            Functionality.getRuningLinesSport(listBoxSportRL);
           // textBox2.MaxLength = int.Parse(Functionality.MaxRLLength);
            comboBox4.Text = "VISOS";
            checkBox3.IsChecked = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Tick);
            timer.Interval = (1000) * (5);             
            timer.Enabled = true;                       
            timer.Start();

            Functionality.fillUpSportsGroup(groupCombo);

            label28.Visibility = Visibility.Hidden;
            textBox17.Visibility = Visibility.Hidden;
            textBox13.Document.PageWidth = 1000;
            //System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            //InputLanguageManager.SetInputLanguage(textBox2, ci);
        }


        void timer_Tick(object sender, ElapsedEventArgs e)
        {
            try
            {
                App.Current.Dispatcher.Invoke((Action)delegate()
                {
                    Functionality.getreports(listBox3);
                });
            }
            catch (Exception ex) { }
        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!textBox1.Text.Equals(""))
            {
                listBox1.Items.Add(new Functionality.Reports() { id = 0, name=textBox1.Text, status="0"});
            }
            textBox1.Text = "";
            button2_Click(this, null);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listBox1.SelectedItem;
                listBox1.Items.RemoveAt(location);
                listBox1.Items.Insert(location - 1, rememberMe);
                listBox1.SelectedIndex = location - 1;
            }

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location < listBox1.Items.Count-1)
            {
                object rememberMe = listBox1.SelectedItem;
                listBox1.Items.RemoveAt(location);
                listBox1.Items.Insert(location + 1, rememberMe);
                listBox1.SelectedIndex = location + 1;
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            System.Collections.IList ls = listBox1.SelectedItems;
            for (int i = 0; i < ls.Count; i++)
            {
                int location = listBox1.Items.IndexOf(ls[i]);
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                if (rp.id != 0)
                {
                    Functionality.removeReportById(rp.id);
                    listBox1.Items.RemoveAt(location);

                }
                else
                {
                    listBox1.Items.RemoveAt(location);
                }
            }
            button2_Click(this, null);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Functionality.saveReports(listBox1);
            Functionality.getreports(listBox1);
        }

        private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location != -1)
            {
                Functionality.RuningLines rl = (Functionality.RuningLines)listBox2.Items[location];
                textBox2.Text=rl.Line;
                comboBox1.Text = rl.Type;
                Functionality.CurrentRLid = rl.id;
                Functionality.CurrentRLindex = location;
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox4.Text.Equals("VISOS"))
            {
                if (MessageBox.Show("Ar tikrai norite trinti visas eilutes?", "Užklausa", MessageBoxButton.YesNo, MessageBoxImage.Asterisk, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    Functionality.removeAllRL();
                    Functionality.getRuningLines(listBox2,"VISOS");
                }
            }
            else
            {
                int i = 0;
                while (i < listBox2.SelectedItems.Count)
                {
                    //int location = listBox2.SelectedIndi
                    Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.SelectedItems[i];//.Items[location];
                    if (rp.id != 0)
                    {
                        //Functionality.removeRuningLineById(rp.id);
                        listBox2.Items.Remove(listBox2.SelectedItems[i]);//.RemoveAt(location);
                        i = 0;

                    }
                    else
                    {
                        listBox2.Items.Remove(listBox2.SelectedItems[i]);//.RemoveAt(location);
                        i = 0;
                    }
                }
               /*int location = listBox2.SelectedIndex;
                Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.Items[location];
                if (rp.id != 0)
                {
                    //Functionality.removeRuningLineById(rp.id);
                    listBox2.Items.RemoveAt(location);

                }
                else
                {
                    listBox2.Items.RemoveAt(location);
                }*/
            }
          /*  System.Collections.IList ls = listBox2.SelectedItems;
            for (int i = 0; i < ls.Count; i++)
            {
                int location = listBox2.Items.IndexOf(ls[i]);
                Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.Items[location];
                if (rp.id != 0)
                {
                    //Functionality.removeReportById(rp.id);
                    listBox2.Items.RemoveAt(location);

                }
                else
                {
                    listBox2.Items.RemoveAt(location);
                }
            }*/
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            Functionality.saveRuningLine(listBox2,textBox2,comboBox1,comboBox4.Text);
            Functionality.getRuningLines(listBox2,comboBox4.Text);
            Functionality.CurrentRLid = 0;
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getreports(listBox3);
        }

        private void tabItem2_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          /*  if (tabControl1.SelectedIndex == 1)
            {
                Functionality.getreports(listBox3);
            }*/
        }

        private void selectReport()
        {
            int location = listBox3.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
               // textBox16.Text = rp.name;
                Functionality.CurrentRPid = rp.id;
                Functionality.CurrentRPstatus = rp.status;
                Functionality.CurrentRPindex = location;

                
            }
        }

        private void selectReportKeyUp(){

            int location = listBox3.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                textBox16.Text = rp.name;
                Functionality.CurrentRPid = rp.id;
                Functionality.CurrentRPstatus = rp.status;
                Functionality.CurrentRPindex = location;

                Functionality.getAnonsaiByRepId(rp.id.ToString(), listBox4);

                groupBox1.IsEnabled = true;
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;


                Functionality.getLydaiByRepId(rp.id.ToString(), listBox6);
                groupBox2.IsEnabled = true;
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;

                groupBox3.IsEnabled = true;
                Functionality.getLogoByRepId(rp.id.ToString(), listBox8);

                groupBox4.IsEnabled = true;
                Functionality.getTitraiByRepId(rp.id.ToString(), listBox9);

                Functionality.setTextRTB(textBox5,"");
                Functionality.setTextRTB(textBox6,"");
                Functionality.setTextRTB(textBox7,"");
                textBox12.Text = "";
                Functionality.setTextRTB(textBox13,"");
                textBox9.Text = "";
                textBox17.Text = "";
                comboBox2.Text = "";
                textBox10.Text = "00:00";
                textBox11.Text = "00:00";
                textBox14.Text = "00:00";
                textBox15.Text = "00:00";
            }
        }

        private void listBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectReport();
        }

        private void listBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];
                Functionality.getAnonsaiPartsByANId(an.id.ToString(), listBox5);
                listBox5.IsEnabled = true;
                textBox4.IsEnabled = true;
                button10.IsEnabled = true;
                button12.IsEnabled = true;
            }
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void button9_Click_1(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox3.Text.Equals(""))
            {
                //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.AddAnonsas(Functionality.CurrentRPid.ToString(), textBox3.Text);
                Functionality.getAnonsaiByRepId(Functionality.CurrentRPid.ToString(), listBox4);
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;
                textBox3.Text = "";
            }
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0 && !textBox4.Text.Equals(""))
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];

                Functionality.AddAnonsasPart(an.id.ToString(), Functionality.rpl(textBox4.Text.ToUpper()));
                Functionality.getAnonsaiPartsByANId(an.id.ToString(), listBox5);
                textBox4.Text = "";
            }
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];
                Functionality.removeAnonsaiById(an.id);
                listBox4.Items.RemoveAt(location);
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;
            }

        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox5.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox5.Items[location];
                Functionality.AnonsaiPartsById(an.id);
                listBox5.Items.RemoveAt(location);

            }
        }

        private void listBox6_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox6.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                listBox7.IsEnabled = true;
                textBox6.IsEnabled = true;
                textBox7.IsEnabled = true;
                button14.IsEnabled = true;
                button15.IsEnabled = true;
                FlowDocument doc5 = new FlowDocument();
                textBox5.Document = doc5;
                textBox5.IsReadOnly = false;
                Paragraph para5 = new Paragraph();
                doc5.Blocks.Add(para5);
                para5.Inlines.Clear();
                para5.Inlines.Add(ld.title);
                bool ch=false;
                if (ld.template.Equals("N")){
                  ch=false;
                }else{
                    ch=true;
                }
                checkBox1.IsChecked = ch;
                Functionality.CurrentLDid = ld.id;
                Functionality.CurrentLDindex = location;


              /*  ListBox parent = (ListBox)sender;
                dragSource = parent;
                object data = (Functionality.Lydai)listBox6.Items[location];// GetDataFromListBox(dragSource, e.GetPosition(parent));

                if (data != null)
                {
                    DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
                }*/
            }

            

        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox5))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
           // string msg = Functionality.SpellCheckText(textBox5);
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !Functionality.getTextRTB(textBox5).Equals(""))
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                Functionality.AddLydai(Functionality.CurrentRPid.ToString(), textBox5, checkBox1);
                Functionality.getLydaiByRepId(Functionality.CurrentRPid.ToString(), listBox6);
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;
                Functionality.setTextRTB(textBox5,"");
                checkBox1.IsChecked = false;
            }
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
           // int location = listBox6.SelectedIndex;
            //if (location >= 0)
             int i = 0;
             while (i < listBox6.SelectedItems.Count)
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.SelectedItems[i];
                Functionality.removeLydaiById(ld.id);
                listBox6.Items.Remove(listBox6.SelectedItems[i]);
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;
            }
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox6) && checkSpell(textBox7))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
            int location = listBox6.SelectedIndex;
            if (location >= 0 && !Functionality.getTextRTB(textBox6).Equals("") && !Functionality.getTextRTB(textBox7).Equals(""))
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];

                Functionality.AddLydaiPart(ld.id.ToString(), Functionality.rpl(Functionality.getTextRTB(textBox7).TrimStart().ToUpper()), Functionality.rpl(Functionality.getTextRTB(textBox6).ToUpper()));
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                Functionality.setTextRTB(textBox6,"");
                Functionality.setTextRTB(textBox7,"");
                Functionality.updateLydaiCount(ld.id.ToString(), listBox7);
            }
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox7.SelectedIndex;
            if (location >= 0)
            {
                Functionality.LydaiParts ld = (Functionality.LydaiParts)listBox7.Items[location];
                Functionality.removeLydaiPartsById(ld.id);
                listBox7.Items.RemoveAt(location);
                Functionality.updateLydaiCount(ld.lead_id.ToString(), listBox7);
            }

        }

       

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0  && !comboBox2.Text.Equals(""))
            {
                if (Functionality.checkTime(textBox10, textBox11))
                {
                //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                int loc = comboBox2.SelectedIndex;
                Functionality.logosTemplates lt = (Functionality.logosTemplates)comboBox2.Items[loc];
                string template = lt.Template;
                checkBox2.IsChecked = true;
                Functionality.AddLogo(Functionality.CurrentRPid.ToString(), lt.TemplateName, textBox9, template, textBox10, textBox11, checkBox2,textBox17);
                Functionality.getLogoByRepId(Functionality.CurrentRPid.ToString(), listBox8);
                textBox9.Text = "";
                textBox10.Text = "00:00";
                textBox11.Text = "00:00";
                textBox17.Text = "";
                checkBox2.IsChecked = false;
                }else{MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!");}
            }
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox13))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox12.Text.Equals("") && !Functionality.getTextRTB(textBox13).TrimStart().TrimEnd().Equals(""))
            {
                if (Functionality.checkTime(textBox14,textBox15))
                {
                    //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                    Functionality.AddTitras(Functionality.CurrentRPid.ToString(), textBox12, textBox13, textBox14, textBox15);
                    Functionality.getTitraiByRepId(Functionality.CurrentRPid.ToString(), listBox9);
                    textBox12.Text = "";
                    Functionality.setTextRTB(textBox13,"");
                    textBox14.Text = "00:00";
                    textBox15.Text = "00:00";
                }else{MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!");}
            }
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            //int location = listBox8.SelectedIndex;
            //if (location >= 0)
            int i = 0;
             while (i < listBox8.SelectedItems.Count)
            {
                Functionality.logo lg = (Functionality.logo)listBox8.SelectedItems[i];
                Functionality.removeLogoById(lg.id);
                listBox8.Items.Remove(listBox8.SelectedItems[i]);
               
            }
        }

        private void button22_Click(object sender, RoutedEventArgs e)
        {
            //int location = listBox9.SelectedIndex;
            //if (location >= 0)
            int i = 0;
             while (i < listBox9.SelectedItems.Count)
            {
                Functionality.Titras tr = (Functionality.Titras)listBox9.SelectedItems[i];
                Functionality.removeTitrasById(tr.id);
                listBox9.Items.Remove(listBox9.SelectedItems[i]);

            }
        }

        private void listBox8_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox8.SelectedIndex;
            if (location != -1)
            {
                Functionality.logo lg = (Functionality.logo)listBox8.Items[location];
                textBox9.Text = lg.tekstas;
                textBox10.Text = lg.start_time;
                textBox11.Text = lg.end_time;
                textBox17.Text = lg.data;
                if (lg.reverse.Equals("True"))
                {
                    checkBox2.IsChecked = true;
                }
                else
                {
                    checkBox2.IsChecked = false;
                }
                string tmp = "";
                int ix = -1;
                Functionality.logosTemplates item=null;
                for (int i=0;i<comboBox2.Items.Count;i++){
                    Functionality.logosTemplates lgt = (Functionality.logosTemplates)comboBox2.Items[i];
                    if (lgt.Template.Equals(lg.template)){
                        tmp = lgt.TemplateName;
                        ix = i;
                        item=lgt;
                    }
                }
               // comboBox2.SetValue(= ix;
               // comboBox2.Text = tmp;
                comboBox2.SelectedItem = item;
                Functionality.CurrentLGid = lg.id;
                Functionality.CurrentLGindex = location;
            }
        }

        private void listBox9_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox9.SelectedIndex;
            if (location != -1)
            {
                Functionality.Titras tr = (Functionality.Titras)listBox9.Items[location];
                textBox12.Text = tr.asmuo;
                Functionality.setTextRTB(textBox13,tr.pareigos);
                textBox14.Text = tr.start_time;
                textBox15.Text = tr.end_time;
                Functionality.CurrentTRid = tr.id;
                Functionality.CurrentTRindex = location;
            }
        }

        private void button21_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox13))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox12.Text.Equals("") && !Functionality.getTextRTB(textBox13).Equals(""))
            {
                if (Functionality.checkTime(textBox14,textBox15))
                {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.EditTitras(textBox12, textBox13, textBox14, textBox15);
                Functionality.getTitraiByRepId(Functionality.CurrentRPid.ToString(), listBox9);
                textBox12.Text = "";
                Functionality.setTextRTB(textBox13,"");
                textBox14.Text = "00:00";
                textBox15.Text = "00:00";
                }
                else { MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!"); }
            }
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0  && comboBox2.SelectedIndex!=-1)
            {
                if (Functionality.checkTime(textBox10, textBox11))
                {
                   // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                    int loc = comboBox2.SelectedIndex;
                    Functionality.logosTemplates lt = (Functionality.logosTemplates)comboBox2.Items[loc];
                    string template = lt.Template;
                    checkBox2.IsChecked = true;
                    Functionality.EditLogo(lt.TemplateName, textBox9, template, textBox10, textBox11, checkBox2,textBox17);
                    Functionality.getLogoByRepId(Functionality.CurrentRPid.ToString(), listBox8);
                    textBox9.Text = "";
                    textBox10.Text = "00:00";
                    textBox11.Text = "00:00";
                    textBox17.Text = "";
                    comboBox2.Text = "";
                    checkBox2.IsChecked = false;
                }
            }
        }

        private void listBox2_LostFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void listBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    int i = 0;
                    while (i < listBox2.SelectedItems.Count)
                    {
                        //int location = listBox2.SelectedIndi
                        Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.SelectedItems[i];//.Items[location];
                        if (rp.id != 0)
                        {
                           // Functionality.removeRuningLineById(rp.id,rp.Type);
                            listBox2.Items.Remove(listBox2.SelectedItems[i]);//.RemoveAt(location);
                            i = 0;

                        }
                        else
                        {
                            listBox2.Items.Remove(listBox2.SelectedItems[i]);//.RemoveAt(location);
                            i = 0;
                        }
                    }
                }
            }
        }

        private void button23_Click(object sender, RoutedEventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            textBox2.Text="";
            Functionality.CurrentRLid = 0;
            Functionality.CurrentRLindex = 0;
            comboBox1.Text = comboBox4.Text;
        }

        private void textBox7_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                textBox6.Focus();
            }
            
        }

        private void button24_Click(object sender, RoutedEventArgs e)
        {
            /*if (checkSpell(textBox2))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }*/
            Functionality.insertRuningLine(listBox2, textBox2, comboBox1);
            Functionality.CurrentRLid = 0;
        }

        private void button26_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listBox2.SelectedItem;
                listBox2.Items.RemoveAt(location);
                listBox2.Items.Insert(location - 1, rememberMe);
                listBox2.SelectedIndex = location - 1;
            }
        }

        private void button25_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location < listBox2.Items.Count - 1)
            {
                object rememberMe = listBox2.SelectedItem;
                listBox2.Items.RemoveAt(location);
                listBox2.Items.Insert(location + 1, rememberMe);
                listBox2.SelectedIndex = location + 1;
            }
        }

        private void comboBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem selectedItem = (ListBoxItem)comboBox4.SelectedItem;
            Functionality.getRuningLines(listBox2, selectedItem.Content.ToString());
            comboBox1.Text = selectedItem.Content.ToString();
        }

        private void Image_ImageFailed_1(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            //MessageBox.Show(e.Source.ToString());
        }

        private void textBox2_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //MessageBox.Show(e.Source.ToString());
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location != -1)
            {
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                textBox1.Text = rp.name;
                Functionality.CurrentRPid = rp.id;
                Functionality.CurrentRPindex = location;
            }
        }

        private void button27_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location >= 0 && !textBox1.Text.Equals(""))
            {
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                
                listBox1.Items.Insert(location,new Functionality.Reports(){id=rp.id,name=textBox1.Text,status=rp.status});
                listBox1.Items.RemoveAt(location+1);   
            // Functionality.EditLogo(textBox8, textBox9, template, textBox10, textBox11, checkBox2);
                //Functionality.getreports(listBox1);
                textBox1.Text = "";
            }
            button2_Click(this, null);
        }

        private void listBox7_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox7.SelectedIndex;
            if (location >= 0)
            {
                Functionality.LydaiParts ldP = (Functionality.LydaiParts)listBox7.Items[location];
                Functionality.setTextRTB(textBox7,ldP.tekstas1);
                Functionality.setTextRTB(textBox6,ldP.tekstas2);
                Functionality.CurrentLDPartsid = ldP.id;
                Functionality.CurrentLDPartsindex = location;
            }
        }

        private void button28_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox5))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !Functionality.getTextRTB(textBox5).Equals("") )
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.EditLydai(textBox5, checkBox1);
                Functionality.getLydaiByRepId(Functionality.CurrentRPid.ToString(), listBox6);
                Functionality.setTextRTB(textBox5,"");
                checkBox1.IsChecked = false;
            }
        }

        private void button29_Click(object sender, RoutedEventArgs e)
        {
            if (checkSpell(textBox6) && checkSpell(textBox7))
            {
                if (MessageBox.Show("Yra klaidų tekste! Ar saugoti klaidingą tekstą?", "Klaidos", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }
            int location = listBox6.SelectedIndex;
            if (location >= 0 && !Functionality.getTextRTB(textBox7).Equals("") && !Functionality.getTextRTB(textBox6).Equals(""))
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];

                Functionality.EditLydaiParts(textBox7, textBox6);
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                Functionality.setTextRTB(textBox7,"");
                Functionality.setTextRTB(textBox6,"");
             }
        }

        private void button33_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getAnonsaiPartsByANId("1", listBox10);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void listBox10_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                textBox8.Text = an.tekstas;
                Functionality.CurrentANPartsid = an.id;
                Functionality.CurrentANPartsindex = location;
            }
        }

        private void button31_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                Functionality.AnonsaiPartsById(an.id);
                listBox10.Items.RemoveAt(location);
                textBox8.Text = "";

            }
        }

        private void button30_Click(object sender, RoutedEventArgs e)
        {
            if (!textBox8.Text.Equals(""))
            {
                Functionality.AddAnonsasPart("1", Functionality.rpl(textBox8.Text.ToUpper()));
                Functionality.getAnonsaiPartsByANId("1", listBox10);
                textBox8.Text = "";
            }
        }

        private void button32_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                Functionality.EditAnonsaiPartsByANid(an.id.ToString(), textBox8.Text.ToUpper());
                Functionality.getAnonsaiPartsByANId("1",listBox10);
                textBox8.Text = "";

            }
        }

        private void Image_ImageFailed_2(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void textBox7_KeyUp(object sender, KeyEventArgs e)
        {
            string allTextThis = Functionality.getTextRTB(textBox7).TrimStart();
            
            string thisText = "";
            string nextText = "";
            if (allTextThis.Length >= 41)
            {
                string[] words = allTextThis.Split(' ');
                thisText = "";
                for (int i=0;i<words.Length-1;i++){
                    thisText = thisText + words[i] + " ";
                }

               Functionality.setTextRTB(textBox7,thisText);

               Functionality.setTextRTB(textBox6,words[words.Length - 1]);
                textBox6.CaretPosition = textBox6.Document.ContentEnd;
                textBox6.Focus();
              //  string allTextNext = Functionality.getTextRTB(textBox6);
               // textBox6.Selection//allTextNext.Length, 0);
            }
            label21.Content = allTextThis.Length.ToString() + "/41";
            if (thisText.Length > 35) { label21.Foreground = Brushes.Red; }
            else { label21.Foreground = Brushes.Black; }

        }

        private void textBox11_KeyUp(object sender, KeyEventArgs e)
        {
          
        }

        private void textBox15_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox15_KeyUp(object sender, KeyEventArgs e)
        {
            changeToTime(textBox15);
        }

        private void changeToTime(TextBox tx)
        {
            string txt = tx.Text;
            if (txt != "")
            {
                if (tx.Text.Length == 2) { tx.Text = tx.Text + ":"; }
                tx.Text = Regex.Replace(tx.Text, "[^0-9:]", "");
                if (txt != tx.Text)
                {
                    tx.Select(tx.Text.Length, 0);
                }
                if (tx.Text.Length > 5)
                { tx.Text = tx.Text.Substring(0, 5); tx.Select(tx.Text.Length, 0); }
                int i = 0;
                i = tx.Text.IndexOf(':', 0);
                if (i > -1)
                {
                    i++;
                    while ((i = tx.Text.IndexOf(':', i)) != -1)
                    {
                        tx.Text = tx.Text.Remove(i, 1);
                        if (i < tx.Text.Length)
                        { i++; }
                        else { break; }
                    }
                }
                tx.Select(tx.Text.Length, 0);
            }
        }

        private void button34_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getreports(listBox1);
        }

        private void Image_ImageFailed_3(object sender, ExceptionRoutedEventArgs e)
        {
            button1_Click(sender, e);
        }

        private void textBox16_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                AddRep();
            }
        }

        private void button35_Click(object sender, RoutedEventArgs e)
        {
            AddRep();
        }

        private void AddRep()
        {
            string name = textBox16.Text;// ((Functionality.Reports)listBox3.SelectedItem).name;
            if (!textBox16.Text.Equals(""))
            {
                listBox3.Items.Add(new Functionality.Reports() { id = 0, name = textBox16.Text, status = "0" });
                Functionality.saveReports(listBox3);
                /*int id = Functionality.getReportIdByName(textBox16.Text);
                int location =-1;
                for(int i=0;i<listBox3.Items.Count;i++){
                  Functionality.Reports rp=(Functionality.Reports)listBox3.Items[i];
                  if (rp.name == textBox16.Text) { location = i; }
                }
                ((Functionality.Reports)listBox3.Items[location]).id = id;*/
                

                
            }
            listBox3.Items.Clear();
            textBox1.Text = "";
            
            Functionality.getreports(listBox3);
            int selix = listBox3.Items.Cast<Functionality.Reports>().ToList().FindIndex(rep => rep.name.Equals(name, StringComparison.Ordinal));
            listBox3.SelectedIndex = selix;
            selectReportKeyUp();

        }

        private void Image_ImageFailed_4(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button36_Click(object sender, RoutedEventArgs e)
        {
                RemoveRep();
        }

        private void listBox3_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                RemoveRep();
            }
            if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                int index = listBox3.SelectedIndex;

                if (index != -1)
                for (int i = 0; i < dragObjectCount; i++) 
                {
                    if (dragType == 1)
                    {
                        Functionality.insertLydas((Functionality.Lydai)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                        listBox3_MouseUp(sender, null);
                    }
                    if (dragType == 2)
                    {
                        Functionality.insertTitras((Functionality.Titras)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                        listBox3_MouseUp(sender, null);
                    }
                    if (dragType == 3)
                    {
                        Functionality.insertLogo((Functionality.logo)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                        listBox3_MouseUp(sender, null);
                    }
                }
            }
        }

        private void RemoveRep()
        {
          
            System.Collections.IList ls = listBox3.SelectedItems;

            if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
            {
                //  for (int i = 0; i < ls.Count; i++)
                //{
                //int location = listBox3.Items.IndexOf(ls[i]);
                /*int location = listBox3.Items.IndexOf(ls[0]);
                Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                if (rp.id != 0)
                {
                    Functionality.removeReportById(rp.id);
                    listBox3.Items.RemoveAt(location);

                }
                else
                {
                    listBox3.Items.RemoveAt(location);
                }*/
                //}

                int i = 0;
                while (i < listBox3.SelectedItems.Count)
                {
                    //int location = listBox2.SelectedIndi
                    Functionality.Reports rp = (Functionality.Reports)listBox3.SelectedItems[i];//.Items[location];
                    if (rp.id != 0)
                    {
                        //Functionality.removeRuningLineById(rp.id);
                        Functionality.removeReportById(rp.id);
                        listBox3.Items.Remove(listBox3.SelectedItems[i]);//.RemoveAt(location);
                        i = 0;

                    }
                    else
                    {
                        listBox3.Items.Remove(listBox3.SelectedItems[i]);//.RemoveAt(location);
                        i = 0;
                    }
                }
            }
        }

        private void checkBox3_Checked(object sender, RoutedEventArgs e)
        {
           
        }

        private void Image_ImageFailed_5(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button37_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox16.Text.Equals(""))
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                listBox3.Items.Insert(location, new Functionality.Reports() { id = Functionality.CurrentRPid, name = textBox16.Text, status = Functionality.CurrentRPstatus });
                listBox3.Items.RemoveAt(location + 1);
                textBox3.Text = "";
            }
            Functionality.saveReports(listBox3);
            Functionality.getreports(listBox3);
        }

        private void checkBox3_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox3.IsChecked == true)
            {
                timer.Start();
            }
            else
            {
                timer.Stop();
            }
        }

        private void listBox3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            selectReportKeyUp();
        }

        //private void selectedReport()
       // {
       //     selectReport();
        //}

        private void listBox3_MouseUpExtracted()
        {
           // selectedReport();
        }

        private void listBox3_MouseUp(object sender, MouseButtonEventArgs e)
        {
            selectReportKeyUp();
        }

        private void textBox6_KeyUp(object sender, KeyEventArgs e)
        {
            string allText = Functionality.getTextRTB(textBox6).TrimStart();
            label22.Content = allText.Length.ToString() + "/41";
            if (allText.Length > 35) { label22.Foreground = Brushes.Red; }
            else { label22.Foreground = Brushes.Black; }
            if (e.Key == Key.Enter)
            {
                button14_Click(sender, null);
            }else{
            
            if (allText.Length > 34)
            {
                Functionality.setTextRTB(textBox6,allText.Substring(0,34));
            }
            }
        }

        private void textBox12_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
            
            if (e.Key == Key.Enter)
            {
                button13_Click(sender, null);
            }else{
                string allText = Functionality.getTextRTB(textBox5).TrimStart();
            if (allText.Length > 34)
            {
                Functionality.setTextRTB(textBox5,allText.Substring(0,34));
            }
            }
        }

        private void textBox13_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button20_Click(sender, null);
            }else if (Functionality.getTextRTB(textBox13).TrimStart().Length>52) {
                Functionality.setTextRTB(textBox13, Functionality.getTextRTB(textBox13).TrimStart().Substring(0, 52));
            }
        }

        private void comboBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button17_Click(sender, null);
            }
        }

        private void listBox9_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    button22_Click(sender, null); textBox12.Text = ""; Functionality.setTextRTB(textBox13,""); textBox14.Text = "00:00"; textBox15.Text = "00:00";
                }
            }
            if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
               // int location = listBox9.SelectedIndex;
                //if (location != -1)
                dragObjectCount = listBox9.SelectedItems.Count;
                dragObject = new object[dragObjectCount];
                for(int i=0;i<dragObjectCount;i++)
                {
                    dragObject[i] = listBox9.SelectedItems[i];// listBox9.Items.GetItemAt(location);
                    dragType = 2;
                }
            }
        }

        private void listBox7_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    button15_Click(sender, null); 
                    Functionality.setTextRTB(textBox6,""); 
                    Functionality.setTextRTB(textBox7,"");
                }
            }
        }

        private void listBox6_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    button16_Click(sender, null); Functionality.setTextRTB(textBox5,"");
                }
            }
            if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                //int location = listBox6.SelectedIndex;
                //if (location != -1)
                dragObjectCount = listBox6.SelectedItems.Count;
                dragObject = new object[dragObjectCount];
                for (int i = 0; i < dragObjectCount; i++)
                {
                    dragObject[i] = listBox6.SelectedItems[i];//.Items.GetItemAt(location);
                    dragType = 1;
                }
            }
        }

        private void listBox8_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    button19_Click(sender, null); textBox9.Text = ""; comboBox2.Text = ""; textBox17.Text = "";
                }
            }
            if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                //int location = listBox8.SelectedIndex;
                //if (location != -1)
                dragObjectCount = listBox8.SelectedItems.Count;
                dragObject = new object[dragObjectCount];
                for (int i = 0; i < dragObjectCount; i++)
                {
                    dragObject[i] = listBox8.SelectedItems[i];//.Items.GetItemAt(location);
                    dragType = 3;
                }
            }
        }

        private static object GetDataFromListBox(ListBox source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);

                    if (data == DependencyProperty.UnsetValue)
                    {
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    }

                    if (element == source)
                    {
                        return null;
                    }
                }

                if (data != DependencyProperty.UnsetValue)
                {
                    return data;
                }
            }

            return null;
        }

        private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            dragSource = parent;
            object data = GetDataFromListBox(dragSource, e.GetPosition(parent));

            if (data != null)
            {
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
            }
        }

        private static bool IsMouseOverTarget(Visual target, Point point)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(target);
            return bounds.Contains(point);
        }

        private void ListBox_Drop(object sender, DragEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            object data = e.Data.GetData(typeof(Functionality.LydaiParts));
            parent.Items.Remove(data);
           // ((IList)dragSource.ItemsSource).Remove(data);
            int index = -1;
            for (int i = 0; i < listBox7.Items.Count; i++)
            {
                var lbi = listBox7.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (lbi == null) continue;
                if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi)))
                {
                    index = i;
                    break;
                }
            }
            parent.Items.Insert(index, data);
        }

        private void ListBox_Drop1(object sender, DragEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            object data = null;
            int obix = 1;
            data = e.Data.GetData(typeof(Functionality.Lydai));
            if (data == null)
            {
                obix = 2;
                data = e.Data.GetData(typeof(Functionality.Titras));
            }
            int index = -1;
            for (int i = 0; i < listBox3.Items.Count; i++)
            {
                var lbi = listBox3.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (lbi == null) continue;
                if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi)))
                {
                    index = i;
                    break;
                }
            }
            
            if (obix == 1)
            {
                Functionality.insertLydas((Functionality.Lydai)data, (Functionality.Reports)listBox3.Items[index]);
            }
            if (obix == 2)
            {
                Functionality.insertTitras((Functionality.Titras)data, (Functionality.Reports)listBox3.Items[index]);
            }
            //parent.Items.Insert(index, data);
        }

        private void listBox7_PreviewMouseMove(object sender, MouseEventArgs e)
        {

        }

        private void textBox14_KeyUp(object sender, KeyEventArgs e)
        {
            changeToTime(textBox14);
        }

        private void textBox10_KeyUp(object sender, KeyEventArgs e)
        {
            changeToTime(textBox10);
        }

        private void textBox11_KeyUp_1(object sender, KeyEventArgs e)
        {
            changeToTime(textBox11);
        }

        private void button38_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int location = listBox7.SelectedIndex;
            if (location > 0)
            {
              /*  int tmp=((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).id;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).id = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location-1)).id;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location - 1)).id = tmp;*/
                string tmps = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas1;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas1 = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location - 1)).tekstas1;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location - 1)).tekstas1 = tmps;
                tmps = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location )).tekstas2;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas2 = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location - 1)).tekstas2;
                ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location - 1)).tekstas2 = tmps;
                listBox7.Items.Refresh();
                Functionality.LydaiParts lp=(Functionality.LydaiParts)listBox7.Items.GetItemAt(location);
               // listBox7.Items.RemoveAt(location);
               // listBox7.Items.Insert(location-1, lp);
                listBox7.SelectedIndex = location - 1;
                Functionality.renumberLydaiParts(listBox7);
            }
            

        }

        private void button39_Click(object sender, System.Windows.RoutedEventArgs e)
        {
             
           int location = listBox7.SelectedIndex;
            //((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas1="wwwwwwwwwwwwww";
           if ((location >= 0) && (location <listBox7.Items.Count-1)) 
           {
              /* int tmp = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).id;
               ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).id = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).id;
               ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).id = tmp;*/
                   string tmps = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas1;
                   ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas1 = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).tekstas1;
                   ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).tekstas1 = tmps;
                   tmps = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas2;
                   ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location)).tekstas2 = ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).tekstas2;
                   ((Functionality.LydaiParts)listBox7.Items.GetItemAt(location + 1)).tekstas2 = tmps;
                   listBox7.Items.Refresh();
                   Functionality.LydaiParts lp = (Functionality.LydaiParts)listBox7.Items.GetItemAt(location);
               //listBox7.Items.RemoveAt(location);
                   //listBox7.Items.Insert(location + 1, lp);
                   listBox7.SelectedIndex = location + 1;
                   Functionality.renumberLydaiParts(listBox7);
           }
           
        }

        private void MenuItem_Click_2(object sender, System.Windows.RoutedEventArgs e)
        {
            //int location = listBox6.SelectedIndex;
            //if (location != -1)
            dragObjectCount = listBox6.SelectedItems.Count;
            dragObject = new object[dragObjectCount];
            for (int i = 0; i < dragObjectCount; i++)
            {
                dragObject[i] = listBox6.SelectedItems[i];//.Items.GetItemAt(location);
                dragType = 1;
            }

        }

        private void MenuItem_Click_3(object sender, System.Windows.RoutedEventArgs e)
        {
            //int location = listBox9.SelectedIndex;
            //if (location != -1)
            dragObjectCount = listBox9.SelectedItems.Count;
            dragObject = new object[dragObjectCount];
            for (int i = 0; i < dragObjectCount; i++)
            {
                dragObject[i] = listBox9.SelectedItems[i];//.Items.GetItemAt(location);
                dragType = 2;
            }
        }

        private void MenuItem_Click_4(object sender, System.Windows.RoutedEventArgs e)
        {
            //int location = listBox8.SelectedIndex;
            //if (location != -1)
            dragObjectCount = listBox8.SelectedItems.Count;
            dragObject = new object[dragObjectCount];
            for (int i = 0; i < dragObjectCount; i++)
            {
                dragObject[i] = listBox8.SelectedItems[i];//.Items.GetItemAt(location);
                dragType = 3;
            }
        }

        private void MenuItem_Click_5(object sender, System.Windows.RoutedEventArgs e)
        {
           /* ListBox parent = (ListBox)sender;
            object data = null;
            int obix = 1;
            data = e.Data.GetData(typeof(Functionality.Lydai));
            if (data == null)
            {
                obix = 2;
                data = e.Data.GetData(typeof(Functionality.Titras));
            }
            int index = -1;
            for (int i = 0; i < listBox3.Items.Count; i++)
            {
                var lbi = listBox3.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (lbi == null) continue;
                if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi)))
                {
                    index = i;
                    break;
                }
            }*/

            int index = listBox3.SelectedIndex;
            if (index!=-1)
            for (int i = 0; i < dragObjectCount; i++) 
            {
            if (dragType == 1)
            {
                Functionality.insertLydas((Functionality.Lydai)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                listBox3_MouseUp(sender, null);
            }
            if (dragType == 2)
            {
                Functionality.insertTitras((Functionality.Titras)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                listBox3_MouseUp(sender, null);
            }
            if (dragType == 3)
            {
                Functionality.insertLogo((Functionality.logo)dragObject[i], (Functionality.Reports)listBox3.Items[index]);
                listBox3_MouseUp(sender, null);
            }
            }
        }

        private void button40_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            GroupEdit gp = new GroupEdit();
            if (gp.ShowDialog() == true)
            {
                Functionality.fillUpSportsGroup(groupCombo);
            }
        }

        private void listBoxSportRL_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBoxSportRL.SelectedIndex;
            if (location != -1)
            {
                Functionality.RuningLinesSport rl = (Functionality.RuningLinesSport)listBoxSportRL.Items[location];
                rlText.Text = rl.textRL;
                int ix = -1;
                for (int i = 0; i < (groupCombo.Items.Count); i++)
                {
                    if (((Functionality.SpGroups)groupCombo.Items[i]).name.Equals(rl.groupName))
                    {
                        ix = i;
                    }
                }
                groupCombo.SelectedIndex = ix;
                team1Text.Text = rl.team1;
                score1Text.Text = rl.score1;
                team2Text.Text = rl.team2;
                score2text.Text = rl.score2;
                Functionality.CurrentRLidSport = rl.id;
                Functionality.CurrentRLindexSport = location;
            }
        }

        private void deleteSportRlButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
              /* int i = 0;
                while (i < listBoxSportRL.SelectedItems.Count)
                {
                    //int location = listBox2.SelectedIndi
                    Functionality.RuningLinesSport rp = (Functionality.RuningLinesSport)listBoxSportRL.SelectedItems[i];//.Items[location];
                    if (rp.id != 0)
                    {
                        //Functionality.removeRuningLineById(rp.id);
                        listBoxSportRL.Items.Remove(listBoxSportRL.SelectedItems[i]);
                        i = 0;

                    }
                    else
                    {
                        listBoxSportRL.Items.Remove(listBoxSportRL.SelectedItems[i]);//.RemoveAt(location);
                        i = 0;
                    }
                }*/
                if (MessageBox.Show("Ar tikrai norite trinti visas eilutes?", "Užklausa", MessageBoxButton.YesNo, MessageBoxImage.Asterisk, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    listBoxSportRL.Items.Clear();
                    Functionality.removeAllRLSport();
                    Functionality.getRuningLinesSport(listBoxSportRL);
                }
        }

        private void saveSportRL_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Functionality.saveRuningLineSport(listBoxSportRL);
            Functionality.getRuningLinesSport(listBoxSportRL);
            Functionality.CurrentRLidSport = 0;
        }

        private void clearButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            groupCombo.Text = "";
            groupCombo.SelectedIndex = -1;
            rlText.Text = "";
            team1Text.Text = "";
            score1Text.Text = "";
            team2Text.Text = "";
            score2text.Text = "";
            Functionality.CurrentRLidSport = 0;
            Functionality.CurrentRLindexSport = 0;
        }

        private void addEditButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Functionality.insertRuningLineSport(listBoxSportRL, rlText.Text.ToUpper(), (Functionality.SpGroups)groupCombo.SelectedValue, team1Text.Text, score1Text.Text, team2Text.Text, score2text.Text);
            Functionality.CurrentRLidSport = 0;
        }

        private void upRLSport_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int location = listBoxSportRL.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listBoxSportRL.SelectedItem;
                listBoxSportRL.Items.RemoveAt(location);
                listBoxSportRL.Items.Insert(location - 1, rememberMe);
                listBoxSportRL.SelectedIndex = location - 1;
            }
        }

        private void downRLSport_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int location = listBoxSportRL.SelectedIndex;
            if (location < listBoxSportRL.Items.Count - 1)
            {
                object rememberMe = listBoxSportRL.SelectedItem;
                listBoxSportRL.Items.RemoveAt(location);
                listBoxSportRL.Items.Insert(location + 1, rememberMe);
                listBoxSportRL.SelectedIndex = location + 1;
            }
        }

        private void listBoxSportRL_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                if (MessageBox.Show("Ar tikrai pašalinti?", "Trynimo patvirtinimas", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                {
                    int i = 0;
                    while (i < listBoxSportRL.SelectedItems.Count)
                    {
                        //int location = listBox2.SelectedIndi
                        Functionality.RuningLinesSport rp = (Functionality.RuningLinesSport)listBoxSportRL.SelectedItems[i];//.Items[location];
                        if (rp.id != 0)
                        {
                            // Functionality.removeRuningLineById(rp.id,rp.Type);
                            listBoxSportRL.Items.Remove(listBoxSportRL.SelectedItems[i]);//.RemoveAt(location);
                            i = 0;

                        }
                        else
                        {
                            listBoxSportRL.Items.Remove(listBoxSportRL.SelectedItems[i]);//.RemoveAt(location);
                            i = 0;
                        }
                    }
                }
            }
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int loc = comboBox2.SelectedIndex;
            if (loc > 0)
            {
                Functionality.logosTemplates lt = (Functionality.logosTemplates)comboBox2.Items[loc];
                if (lt.TemplateName.ToUpper().Equals("ARCHYVAS DATA"))
                {
                    label28.Visibility = Visibility.Visible;
                    textBox17.Visibility = Visibility.Visible;
                }
                else
                {
                    label28.Visibility = Visibility.Hidden;
                    textBox17.Visibility = Visibility.Hidden;
                }
            }
        }

        private void textBox6_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        public bool checkSpell(RichTextBox rtb)
        {
            bool found = false;
            using (Hunspell hunspell = new Hunspell("lt.aff", "lt.dic"))
            {
                // Process all your words here, don't create the Hunspell object for every word.
                TextRange allTextRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
                string allText = allTextRange.Text.Replace("\r\n", "");
                string[] words = allText.Split(' ');

                FlowDocument doc = new FlowDocument();
                rtb.Document = doc;
                rtb.IsReadOnly = false;

                Paragraph para = new Paragraph();
                doc.Blocks.Add(para);
                foreach (string wd in words)
                {
                    bool correct = hunspell.Spell(wd);
                    if (!correct)
                    {
                        found = true;
                        Hyperlink link = new Hyperlink();
                        link.IsEnabled = true;
                        link.Style = (Style)Application.Current.Resources["redhyp"];
                        link.Inlines.Add(wd);
                        ContextMenu contextMenu = new ContextMenu();
                        List<string> suggestions = hunspell.Suggest(wd);
                        foreach (string w in suggestions)
                        {
                            MenuItem item = new MenuItem();

                            item.Header = w;
                            item.Tag = wd + "_" + rtb.Name;
                            item.Click += new RoutedEventHandler(click_item);

                            contextMenu.Items.Add(item);
                        }
                        link.ContextMenu = contextMenu;

                        if (!new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text.Replace("\r\n", "").Equals("")) { para.Inlines.Add(" "); }
                        para.Inlines.Add(link);
                    }
                    else
                    {
                        if (!new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text.Replace("\r\n", "").Equals("")) { para.Inlines.Add(" "); }
                        para.Inlines.Add(wd);
                    }
                }
            }
            return found;
        }

        public void click_item(object sender, RoutedEventArgs e)
        {

            MenuItem mi = (MenuItem)sender;
            string[] vars = mi.Tag.ToString().Split('_');
            string oldword = vars[0];
            string richtextboxname = vars[1];
            RichTextBox rtb = (RichTextBox)Application.Current.MainWindow.FindName(richtextboxname);
            //RichTextBox rtb = FindChild<RichTextBox>(Application.Current.MainWindow, richtextboxname);
            TextRange allTextRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
            string allText = allTextRange.Text.Replace("\r\n", "").Replace(oldword, mi.Header.ToString());

            FlowDocument doc = new FlowDocument();
            rtb.Document = doc;
            rtb.IsReadOnly = false;

            Paragraph para = new Paragraph();
            doc.Blocks.Add(para);
            para.Inlines.Add(allText.TrimStart());
            checkSpell(rtb);
        }
    }
}
